package com.hxjy.struts2.action;


import java.sql.Timestamp;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.hxjy.dao.EmployeeDao;
import com.hxjy.entity.Employee;
import com.hxjy.util.DateUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@Controller
@Scope("prototype")
public class LoginAction extends ActionSupport implements ServletRequestAware{
	private static Log log = LogFactory.getLog(LoginAction.class);
	private EmployeeDao employeeDao;
	private Employee employee;
	private String userName;
	private String password;
	private String error_msg;
	private HttpServletRequest request;
	public String login() {
		employee = employeeDao.Login(userName, password);
		if (employee != null && employee.getUserName() != null && !employee.getPassword().equals("")) {
			log.info(userName + "登陆了!");
			Map<String,Object> map = ActionContext.getContext().getSession();
			map.put("loginUser", employee);
			employee.setLastLoginIp(request.getRemoteAddr());
			employee.setLastLoginTime(new Timestamp(DateUtil.getCurrLongDate().getTime()));
			employeeDao.updateEmployee(employee);

			return "main";
		} else {
			log.info(userName + "登陆出错！");
			error_msg = "用户或密码错误，请重新登陆！";
			return "loginPage";
		}
	}
	public String loginOut(){
		ActionContext.getContext().getSession().remove("loginUser");
		return "loginPage";
	}
	public String jsonTest(){
		System.out.println("-------jsonTest------");
		employee = new Employee();
		employee.setId(1L);
		employee.setUserName("libin");
		employee.setPassword("1111");
		employee.setRealName("李彬");
		return "jsonDemo";
	}
	@Resource
	public void setEmployeeDao(EmployeeDao employeeDao) {
		this.employeeDao = employeeDao;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getError_msg() {
		return error_msg;
	}

	public void setError_msg(String error_msg) {
		this.error_msg = error_msg;
	}
	public Employee getEmployee() {
		return employee;
	}
	
	public void setEmployee(Employee employee) {
		this.employee = employee;
	}
	public EmployeeDao getEmployeeDao() {
		return employeeDao;
	}
	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request=request;
		
	}

}
